package com.example.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * datetime : 2020/10/6 21:28
 * author : shenxiang
 */
@Configuration
@EnableRabbit
@ComponentScan("com.example")
public class RabbitConfig {
    @Bean
    public Queue payBizQueue(){
        // 正常业务队列
        Map<String,Object> props = new HashMap<>();
        // 消息的生存时间 10s
        props.put("x-message-ttl",10000);
        // 消息队列的生存时间 60s
        //props.put("x-expires",60000);
        // 设置该队列所关联的私信交换器(当队列消息TTL到期后依然没有消费,则加入私信队列)
        props.put("x-dead-letter-exchange","ex.pay.dlx");
        props.put("x-dead-letter-routing-key","key.pay.dlx");
        return new Queue("queue.pay.biz",true,false,false,props);
    }

    @Bean
    public Queue payDlxQueue(){
        // 死信队列
        return new Queue("queue.pay.dlx",true,false,false,null);
    }

    @Bean
    public Exchange payBizExchange(){
        // 正常业务交换器
        return new DirectExchange("ex.pay.biz",true,false,null);
    }

    @Bean
    public Exchange payDlxExchange(){
        // 死信交换器
        return new DirectExchange("ex.pay.dlx",true,false,null);
    }

    @Bean
    public Binding payBizBinding(){
        // 正常业务的绑定key
        return BindingBuilder.bind(payBizQueue()).to(payBizExchange()).with("key.pay.biz").noargs();
    }

    @Bean
    public Binding payDlxBinding(){
        // 死信队列的绑定key
        return BindingBuilder.bind(payDlxQueue()).to(payDlxExchange()).with("key.pay.dlx").noargs();
    }

    @Bean
    @Autowired
    public RabbitAdmin rabbitAdmin(ConnectionFactory factory){
        return new RabbitAdmin(factory);
    }
    @Bean
    @Autowired
    public RabbitTemplate rabbitTemplate(ConnectionFactory factory){
        return new RabbitTemplate(factory);
    }

    @Bean
    @Autowired
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
        return factory;
    }

//    @Bean
//    @Autowired
//    public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory){
//        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
//        container.setConnectionFactory(connectionFactory);
//        container.setQueueNames("queue.pay.biz");
//        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
//        container.setMessageListener((ChannelAwareMessageListener)(message,channel) ->{
//            System.out.println("接收到消息");
//            String orderNo = new String(message.getBody(), message.getMessageProperties().getContentEncoding());
//            System.out.println(orderNo);
//            if (message.getMessageProperties().getHeaders().get("error") == null){
//                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//                System.out.println("消息已经确认");
//            }else{
//                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
//                System.out.println("消息被拒绝");
//            }
//        });
//        return container;
//    }
}
